package com.jjwxc.jwjskandriod.framework.utils;

import java.io.File;


/**
 * 文件工具类
 */
public class FileUitl {

	public static long removeAll(File file_parent) {
		if (file_parent.isFile()) {
			return 0;
		}
		File[] files = file_parent.listFiles();
		if (files == null) {
			return 0;
		}
		long size = 0;
		for (File file : files) {
			if (file.isDirectory()) {
				size += removeAll(file);
				file.delete();
			} else {
				size += file.length();
				file.delete();
			}
		}
		return size;
	}







	// 删除目录下所有文件
	public static void deleteAll(String path, boolean deleteDir) {
		File file = new File(path);
		deleteAll(file, deleteDir);
	}

	/**
	 * 删除所有
	 * 
	 * @param file
	 * @param deleteDir
	 */
	public static void deleteAll(File file, boolean deleteDir) {
		if (!file.exists())
			return;
		if (file.isFile() || (file.list().length == 0 && deleteDir)) {
			file.delete();
		} else {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				deleteAll(files[i], true);
				files[i].delete();
			}
			if (file.exists() && deleteDir) // 如果文件本身就是目录 ，就要删除目录
				file.delete();
		}
	}

	// 创建目录
	public static String createDir(String parentPath, String childDirName) {
		File parent = new File(parentPath);
		if (parent.exists() && parent.isDirectory()) {
			File child = new File(parentPath + "/" + childDirName);
			if (!child.exists() && child.mkdir()) {
				return child.getAbsolutePath();
			}
			if (child.exists() && child.isDirectory()) {
				return child.getAbsolutePath();
			}
		}
		return null;
	}

	/**
	 * 生成文件夹
	 * 
	 * @param path
	 * @return
	 */

	public static String createDir(String path) {
		File dir = new File(path);
		if (dir.exists() && dir.isDirectory()) {
			return path;
		}
		if (dir.mkdirs()) {
			return dir.getAbsolutePath();
		}
		return null;
	}

	// 使用url生成文件名称
	public static String fileNameFromURL(String url) {
		if (url != null) {
			return String.valueOf(url.hashCode());
		}
		return null;
	}





}
